package com.example.demo.service.impl;

import com.example.demo.dto.RoutingRuleDTO;
import com.example.demo.entity.RoutingRule;
import com.example.demo.mapper.RoutingRuleMapper;
import com.example.demo.request.CreateRoutingRuleRequest;
import com.example.demo.service.RoutingRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoutingRuleServiceImpl implements RoutingRuleService {

    @Autowired
    private RoutingRuleMapper routingRuleMapper;

    @Override
    public List<RoutingRuleDTO> getRoutingRules(int page, int size) {
        List<RoutingRule> routingRules = routingRuleMapper.selectAll();
        return routingRules.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public long countRoutingRules() {
        return routingRuleMapper.count();
    }

    @Override
    public RoutingRuleDTO getRoutingRuleById(Integer id) {
        RoutingRule routingRule = routingRuleMapper.selectById(id);
        return convertToDTO(routingRule);
    }

    @Override
    public RoutingRule createRoutingRule(CreateRoutingRuleRequest request) {
        RoutingRule routingRule = new RoutingRule();
        routingRule.setRoleConfigId(request.getRoleConfigId());
        routingRule.setRoleName(request.getRoleName());
        routingRule.setDeptId(request.getDeptId());
        routingRule.setDeptName(request.getDeptName());
        routingRule.setFieldValues(request.getFieldValues());
        routingRule.setAssignedUsers(request.getAssignedUsers());
        routingRuleMapper.insert(routingRule);
        return routingRule;
    }

    @Override
    public void updateRoutingRule(Integer id, CreateRoutingRuleRequest request) {
        RoutingRule routingRule = routingRuleMapper.selectById(id);
        routingRule.setRoleConfigId(request.getRoleConfigId());
        routingRule.setRoleName(request.getRoleName());
        routingRule.setDeptId(request.getDeptId());
        routingRule.setDeptName(request.getDeptName());
        routingRule.setFieldValues(request.getFieldValues());
        routingRule.setAssignedUsers(request.getAssignedUsers());
        routingRuleMapper.updateById(routingRule);
    }

    @Override
    public void deleteRoutingRule(Integer id) {
        routingRuleMapper.deleteById(id);
    }

    @Override
    public List<RoutingRuleDTO> searchByRoleName(String roleName) {
        List<RoutingRule> routingRules = routingRuleMapper.selectByRoleNameLike(roleName);
        return routingRules.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public List<RoutingRuleDTO> searchByDeptName(String deptName) {
        List<RoutingRule> routingRules = routingRuleMapper.selectByDeptNameLike(deptName);
        return routingRules.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private RoutingRuleDTO convertToDTO(RoutingRule routingRule) {
        return RoutingRuleDTO.fromEntity(routingRule);
    }
}